package com.fjrz.bbxwj.main.video.adapter

import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fjrz.bbxwj.R
import com.fjrz.bbxwj.entity.CommentChild
import com.fjrz.bbxwj.entity.CommentGroup
import com.module.lemlin.base.loadCircleImage


class DiscussGroupAdapter :
    BaseQuickAdapter<CommentGroup, BaseViewHolder>(R.layout.item_discuss_group) {

    init {
        addChildClickViewIds(R.id.clDiscussGroup, R.id.tvDiscussGroupMore, R.id.tvDiscussGroupGreat)
    }


    private var mOnItemClickCommentChildListener: OnItemClickCommentChildListener? = null

    override fun convert(holder: BaseViewHolder, item: CommentGroup) {

        holder.getView<RecyclerView>(R.id.rvDiscussGroupBody).adapter =
            DiscussChildAdapter().apply {
                item.latest_child_comment?.let {
                    if (item.commentChildList.isNullOrEmpty()) {
                        item.setCommentChildItem(mutableListOf(it))
                    }
                }
                setNewInstance(item.commentChildList)
                setOnItemClickListener { adapter, view, position ->
                    mOnItemClickCommentChildListener?.onItemClick(view, position, getItem(position))
                }
            }

        holder.setText(R.id.tvDiscussGroupName, item.user.nickname)
            .setText(R.id.tvDiscussGroupTime, TimeUtils.millis2String(item.createtime * 1000))
            .setText(R.id.tvDiscussGroupContent, item.content)
            .setText(R.id.tvDiscussGroupGreat, item.likes.toString())
            .setText(
                R.id.tvDiscussGroupMore,
                StringBuilder().append("展开").append(item.child_comments_count).append("条回复")
            )
            .setGone(
                R.id.tvDiscussGroupMore,
                item.child_comments_count == item.commentChildList?.size ?: 0
            )
            .getView<ImageView>(R.id.ivDiscussGroupAvatar).loadCircleImage(item.user.avatar)


        val drawable: Drawable = if (item.like == 1)
            context.resources.getDrawable(R.mipmap.ic_17545)
        else context.resources.getDrawable(R.mipmap.ic_17331)
        drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        holder.getView<TextView>(R.id.tvDiscussGroupGreat)
            .setCompoundDrawables(null, drawable, null, null)

    }

    fun setOnItemClickCommentChildListener(listener: OnItemClickCommentChildListener?) {
        this.mOnItemClickCommentChildListener = listener
    }

    interface OnItemClickCommentChildListener {
        fun onItemClick(
            view: View, position: Int, commentChild: CommentChild
        )
    }
}


class DiscussChildAdapter :
    BaseQuickAdapter<CommentChild, BaseViewHolder>(R.layout.item_discuss_child) {
    override fun convert(holder: BaseViewHolder, item: CommentChild) {
        holder.setText(R.id.tvDiscussChildName, item.user.nickname)
            .setText(R.id.tvDiscussChildTime, TimeUtils.millis2String(item.createtime * 1000))
            .setText(R.id.tvDiscussChildContent, item.content)
            .setText(
                R.id.tvDiscussChildAtName,
                if (item.at_user == null) "" else " 回复·${item.at_user.nickname}"
            )
            .setGone(R.id.tvDiscussChildAtName, item.at_user == null)
            .getView<ImageView>(R.id.ivDiscussChildAvatar).loadCircleImage(item.user.avatar)
    }
}